package information.details;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import exception.DataLengthException;
import factory.MyJFactory;

public abstract class AbstractDetails extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private JPanel labelPanel = new JPanel();;
	private JPanel textPanel = new JPanel();
	
	private JLabel titleLabel = new JLabel();
	
	protected JTextField[] textFields;

	private JLabel[] labels;
	private ArrayList<JLabel> extraLabels = new ArrayList<JLabel>();
	private ArrayList<JTextField> extraTexts = new ArrayList<JTextField>();
	
	public AbstractDetails(int width) {
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.add(titleLabel);
		
		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		contentPanel.setPreferredSize(new Dimension(780/3, 400));
		
		labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.Y_AXIS));
		contentPanel.add(labelPanel);
		
		textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
		contentPanel.add(textPanel);
		
		this.add(contentPanel);
	}
	
	public JPanel getLabelPanel() {
		return labelPanel;
	}
	
	public void setTitle(String title) {
		titleLabel.setText(title);
	}
	
	public void setAllPrimaryTexts(Object[] data) {
		for (int i = 0; i < data.length; i++) {
			String text = data[i] != null ? data[i].toString() : "";
			this.textFields[i].setText(text);
		}
	}
	
	public void addExtraDetailRow(Object[] data) {
		if (data.length != textFields.length)
			throw new DataLengthException("Data length " + data.length + " != " + textFields.length);
		
		JLabel tempLabel = new JLabel(" ");
		JTextField tempField = MyJFactory.createTextField(" ");
		
		this.extraLabels.add(tempLabel);
		this.labelPanel.add(tempLabel);
		
		this.extraTexts.add(tempField);
		this.textPanel.add(tempField);	
		
		for (int i = 0; i < labels.length; i++) {
			tempLabel = new JLabel(labels[i].getText());
			this.extraLabels.add(tempLabel);
			this.labelPanel.add(tempLabel);
			
			String text = data[i] != null ? data[i].toString() : "";
			tempField = MyJFactory.createTextField(text);
			this.extraTexts.add(tempField);
			this.textPanel.add(tempField);
		}
	}
	
	public void clear() {
		for (int i = 0; i < this.textFields.length; i++)
			textFields[i].setText("");
		
		for (int i = extraLabels.size(); i > 0 ; i--) {
			labelPanel.remove(extraLabels.remove(i-1));
			textPanel.remove(extraTexts.remove(i-1));
		}
	}
	
	protected void setPrimaryTextAt(int i, Object data) {
		String text = data != null ? data.toString() : "";
		this.textFields[i].setText(text); 
	}
	
	protected void setupTextFields(JTextField[] textFields) {
		this.textFields = textFields;
		
		for (int i = 0; i < this.textFields.length; i++) {
			this.textFields[i] = MyJFactory.createTextField("");
			this.textPanel.add(textFields[i]);
		}
	}
	
	protected void addLabels(JLabel[] labels) {
		this.labels = labels;
		
		for (int i = 0; i < labels.length; i++) 
			this.labelPanel.add(labels[i]);
	}
	
	protected JLabel[] getLabelArray() {
		return labels;
	}
	
	abstract protected void createTextFieldArray();
	abstract protected void setupLabelArray();
}
